/**
 * Copyright (c) 2008 Luciano Bustos - riaevolution.com, All Rights Reserved.
 *
 * Contact luchyx@riaevolution.com
 * See www.riaevolution.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * */
package com.riaevolution.components.iconhelp
{
	import flash.display.DisplayObject;
	import flash.filters.DropShadowFilter;
	import flash.text.TextLineMetrics;

	import mx.core.UIComponent;
	import mx.core.UITextField;
	import mx.styles.CSSStyleDeclaration;
	import mx.styles.StyleManager;

	/**
	 * Component created when the user roll over the iconHelp icon.
	 * This component is more advanced than the ToolTip class.
	 * @see com.riaevolution.components.iconhelp.HelpIcon
	 */
	public class HelpIconWindow extends UIComponent implements IHelpIconWindow
	{

		public function HelpIconWindow()
		{
			super();
		}

		private var _titleUI:UITextField;
		private var _footerUI:UITextField;

		private var _contentUI:UITextField;

		private var _titleChanged:Boolean=false;
		private var _title:String;

		/**
		 * Initialize default styles
		 */
		private static var classConstructed:Boolean=classConstruct();


		/**
		 * Initialize default styles
		 * Styles initialization
		 */
		private static function classConstruct():Boolean
		{
			// Check if a CSSStyleDeclaration exists for the component
			var style:CSSStyleDeclaration=StyleManager.getStyleDeclaration("HelpIconWindow");
			var bgColor:Number=StyleManager.getStyleDeclaration("ToolTip").getStyle("backgroundColor");
			var brColor:Number=StyleManager.getStyleDeclaration("ToolTip").getStyle("borderColor");
			var radius:Number=StyleManager.getStyleDeclaration("ToolTip").getStyle("cornerRadius");
			//text color
			var color:Number=StyleManager.getStyleDeclaration("ToolTip").getStyle("color");
			if (!style)
			{
				// If not, create a new one
				style=new CSSStyleDeclaration();
				// and set style default values using an inline function
				style.defaultFactory=function():void
				{
					// Template:
					// this.<myStyle> = <styleDefaultValue>;
					this.borderColor=brColor;
					this.backgroundColor=bgColor;
					this.color=color;
					this.cornerRadius=radius;
					//this.normalAlpha = .5;
				};

				// Assign the style declaration
				StyleManager.setStyleDeclaration("HelpIconWindow", style, true);
			}
			else
			{
				// Ensure that default values exist for all our style properties
				if (style.getStyle("borderColor") == undefined)
				{
					style.setStyle("borderColor", brColor);
				}
				if (style.getStyle("backgroundColor") == undefined)
				{
					style.setStyle("backgroundColor", bgColor);
				}
				if (style.getStyle("cornerRadius") == undefined)
				{
					style.setStyle("cornerRadius", radius);
				}
				if (style.getStyle("color") == undefined)
				{
					style.setStyle("color", color);
				}
			}
			return true;
		}

		/**
		 * Title information
		 * @return
		 */
		public function get title():String
		{
			return _title;
		}

		public function set title(value:String):void
		{
			_title=value;
			_titleChanged=true;
			invalidateProperties();
		}

		private var _descriptionChanged:Boolean=false;
		private var _description:String;

		/**
		 * @inheritDoc
		 */
		public function get description():String
		{
			return _description;
		}

		/**
		 * @inheritDoc
		 */
		public function set description(value:String):void
		{
			_description=value;
			_descriptionChanged=true;
			invalidateProperties();
		}



		private var _footerChanged:Boolean=false;
		private var _footer:String="&#169; riaevolution.com";

		/**
		 * set the footer content
		 */
		public function get footer():String
		{
			return _footer;
		}

		/**
		 * @inheritDoc
		 */
		public function set footer(value:String):void
		{
			_footer=value;
			_footerChanged=true;
			invalidateProperties();
		}

		private var _iconWidth:Number=0;

		public function get iconWidth():Number
		{
			return _iconWidth;
		}

		public function set iconWidth(value:Number):void
		{
			_iconWidth=value;
		}

		private var _iconHeight:Number=0;

		public function get iconHeight():Number
		{
			return _iconHeight;
		}

		/**
		 * set the icon to display
		 */
		public function set iconHeight(value:Number):void
		{
			_iconHeight=value;
		}

		/**
		 * @inheritDoc
		 */
		public function set icon(value:DisplayObject):void
		{
			addChildAt(value, this.numChildren);
		}


		private var _borderVisible:Boolean=false;

		/**
		 * @inheritDoc
		 */
		override protected function createChildren():void
		{
			super.createChildren();

			_titleUI=new UITextField();
			_titleUI.name="title";
			_titleUI.autoSize="left";
			_titleUI.selectable=true;
			_titleUI.border=_borderVisible;
			addChild(_titleUI);

			_contentUI=new UITextField();
			_contentUI.name="content";
			_contentUI.selectable=true;
			_contentUI.mouseWheelEnabled=false;
			_contentUI.autoSize="left";
			_contentUI.border=_borderVisible;
			_contentUI.multiline=true;
			addChild(_contentUI);

			_footerUI=new UITextField();
			_footerUI.name="footer";
			_footerUI.selectable=true;
			_footerUI.border=_borderVisible;
			_footerUI.autoSize="left";
			_footerUI.htmlText=_footer;
			addChild(_footerUI);

			_contentUI.textColor=getStyle("color");
			_titleUI.textColor=getStyle("color");

			drawArea();
		}

		/**
		 * @inheritDoc
		 */
		override protected function commitProperties():void
		{
			super.commitProperties();
			var redraw:Boolean=false;
			if (_titleChanged)
			{
				_titleChanged=false;
				_titleUI.htmlText="<b>" + title + "</b>";
				redraw=true;
			}
			if (_descriptionChanged)
			{
				_descriptionChanged=false;
				_contentUI.htmlText=this.description;
				redraw=true;
			}
			if (_footerChanged)
			{
				_footerChanged=false;
				_footerUI.htmlText=this.footer;
				redraw=true;
			}
			if (redraw)
			{
				drawArea();
			}
		}

		/**
		 * draw the tooltip window
		 */
		protected function drawArea():void
		{

			var ln:TextLineMetrics=measureHTMLText(this.description);

			_titleUI.x=iconWidth + 2;
			_titleUI.y=2;

			_contentUI.x=2;
			_contentUI.y=Math.max(iconHeight, _titleUI.textHeight) + 2;


			var uiwidth:Number=Math.max(_contentUI.textWidth + 70, _titleUI.textWidth + 70);
			var uih:Number=Math.max(_contentUI.textHeight + _titleUI.textHeight, _contentUI.height + _titleUI.height) + _footerUI.textHeight + 4;

			if (uiwidth > 420)
			{
				_contentUI.wordWrap=true;
			}
			uiwidth=Math.min(uiwidth, 420);
			_contentUI.width=uiwidth - 8;
			if (_contentUI.wordWrap)
			{
				uih=Math.max(_contentUI.measuredMinHeight + _titleUI.textHeight, _contentUI.height + _titleUI.height) + iconHeight + _footerUI.textHeight + 8;
			}

			_footerUI.x=uiwidth - _footerUI.textWidth - 8;
			_footerUI.y=_contentUI.height + _footerUI.height + 2;


			var cornerRadius:Number=getStyle("cornerRadius");

			graphics.clear();
			graphics.beginFill(getStyle("backgroundColor"), 1);
			graphics.lineStyle(1, getStyle("borderColor"));
			filters=[new DropShadowFilter()];
			graphics.drawRoundRect(-1, -1, uiwidth, uih, cornerRadius, cornerRadius);
			graphics.endFill();
		}
	}
}